// pages/shopInfo/shopInfo.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopInfo: null,
        shopGoods: [],
        agentGoods: [],
        shopVideo: [],
        shopComment: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getOnOff()
        this.getShopInfo()
        this.getAgentGoods()
        this.getShopGoods()
        this.getShopVideo()
        this.getShopComment()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            shopInfo: null,
            shopGoods: [],
            shopComment: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getShopComment()
    },

    getOnOff: function() {
        const that = this
        common.post('Home/index', {
            activity: 785
        }).then(function(res) {
            if (res.code == 204) {
                return
            }
            if (res.show_video == app.globalData.videoShow) {
                that.setData({
                    videoShow: true
                })
            } else {
                that.setData({
                    videoShow: false
                })
            }
        }).catch(function(e) {})
    },

    getShopInfo: function() {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId')
        }
        common.post('Store/get_store_info', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopInfo: res
                })
            }
        }).catch(function(e) {})
    },

    getShopGoods: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            recommend: 1,
            is_sale: '1',
            is_goods: '0',
            client: 2,
            page: 0,
            limit: 10
        }
        common.post('Goods/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopGoods: res
                })
            }
        }).catch(function(e) {})
    },

    getAgentGoods: function() {
        const that = this
        let data = {
            activity: 785,
            store_id: 2,
            is_sale: '1',
            is_goods: '1',
            agency_goods: '1',
            page: 0,
            limit: 1,
            client: 2
        }
        common.post('Goodslist/goods_list', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                agentGoods: res
            })
        }).catch(function(e) {})
    },

    getShopVideo: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId') ? wx.getStorageSync('storeId') : 2,
            page: 0,
            limit: 2
        }
        common.post('Video/video_list', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    shopVideo: res
                })
            }
        }).catch(function(e) {})
    },

    getShopComment: function() {

    },

    goGoodsDetail: function(e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },

    goVideoDetail: function(e) {
        wx.navigateTo({
            url: '/pages/shopVideo/shopVideoDetail/shopVideoDetail?videoId=' + e.currentTarget.dataset.id
        })
    },

    goGoodsMore: function() {
        wx.navigateTo({
            url: '/pages/shopGoods/shopGoods?classId=all'
        })
    },

    goVideoMore: function() {
        wx.navigateTo({
            url: '/pages/shopVideo/shopVideo/shopVideo'
        })
    }
})